#ifndef BTL_EXTRA_VIDEOSOURCE_VIDEOSOURCEV4L2
#define BTL_EXTRA_VIDEOSOURCE_VIDEOSOURCEV4L2

#include <btl/extra/VideoSource/VideoSource.hpp>
//#include <linux/videodev.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>


#define DEFAULT_V4L2_REQUESTED_NUM_BUFFERS 5


namespace btl
{
namespace extra
{
namespace videosource
{

class VideoSourceV4L2 : public VideoSource
{

   public:

      VideoSourceV4L2(const char* device, int requestedNumBuffers = DEFAULT_V4L2_REQUESTED_NUM_BUFFERS);

      ~VideoSourceV4L2();

      const ImageRegionConstRGB getNextFrame();

   protected:

      void yuv444_to_rgb888(unsigned char y, unsigned char u, unsigned char v, unsigned char* r, unsigned char* g, unsigned char* b);

      int _fd;

      //struct v4l2_capability _cap;

      //struct v4l2_format _format;

      //struct v4l2_requestbuffers _reqbufs;

      struct FrameBuffer
      {
         void* data;
         size_t length;
      };

      FrameBuffer* _buffers;

      int _numBuffers;

      struct Exception : public VideoSource::Exception
      {
         Exception(const std::string& str) : VideoSource::Exception(str) {}
      };

};

} //namespace videosource
} //namespace extra
} //namespace btl


namespace btl
{
namespace extra
{

using videosource::VideoSourceV4L2;

} //namespace extra
} //namespace btl


#endif //BTL_EXTRA_VIDEOSOURCE_VIDEOSOURCEV4L2
